3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
19 * @ingroup RevisionDelete
22 use MediaWiki\MediaWikiServices
;
25 * Abstract base class for a list of deletable items. The list class
26 * needs to be able to make a query from a set of identifiers to pull
27 * relevant rows, to return RevDelItem subclasses wrapping them, and
28 * to wrap bulk update operations.
30 abstract class RevDelList
extends RevisionListBase
{
31 function __construct( IContextSource
$context, Title
$title, array $ids ) {
32 parent
::__construct( $context, $title );
37 * Get the DB field name associated with the ID list.
38 * This used to populate the log_search table for finding log entries.
39 * Override this function.
42 public static function getRelationType() {
47 * Get the user right required for this list type
48 * Override this function.
52 public static function getRestriction() {
57 * Get the revision deletion constant for this list type
58 * Override this function.
62 public static function getRevdelConstant() {
67 * Suggest a target for the revision deletion
68 * Optionally override this function.
70 * @param Title|null $target User-supplied target
74 public static function suggestTarget( $target, array $ids ) {
79 * Indicate whether any item in this list is suppressed
83 public function areAnySuppressed() {
84 $bit = $this->getSuppressBit();
86 /** @var RevDelItem $item */
87 foreach ( $this as $item ) {
88 if ( $item->getBits() & $bit ) {
97 * Set the visibility for the revisions in this list. Logging and
98 * transactions are done here.
100 * @param array $params Associative array of parameters. Members are:
101 * value: ExtractBitParams() bitfield array
102 * comment: The log comment
103 * perItemStatus: Set if you want per-item status reports
104 * tags: The array of change tags to apply to the log entry
106 * @since 1.23 Added 'perItemStatus' param
108 public function setVisibility( array $params ) {
109 global $wgActorTableSchemaMigrationStage;
111 $status = Status
::newGood();
113 $bitPars = $params['value'];
114 $comment = $params['comment'];
115 $perItemStatus = $params['perItemStatus'] ??
false;
117 // CAS-style checks are done on the _deleted fields so the select
118 // does not need to use FOR UPDATE nor be in the atomic section
119 $dbw = wfGetDB( DB_MASTER
);
120 $this->res
= $this->doQuery( $dbw );
122 $status->merge( $this->acquireItemLocks() );
123 if ( !$status->isGood() ) {
127 $dbw->startAtomic( __METHOD__
);
128 $dbw->onTransactionResolution(
130 // Release locks on commit or error
131 $this->releaseItemLocks();
136 $missing = array_flip( $this->ids
);
137 $this->clearFileOps();
139 $authorIds = $authorIPs = $authorActors = [];
141 if ( $perItemStatus ) {
142 $status->itemStatuses
= [];
145 // For multi-item deletions, set the old/new bitfields in log_params such that "hid X"
146 // shows in logs if field X was hidden from ANY item and likewise for "unhid Y". Note the
147 // form does not let the same field get hidden and unhidden in different items at once.
152 // Will be filled with id => [old, new bits] information and
153 // passed to doPostCommitUpdates().
154 $visibilityChangeMap = [];
156 /** @var RevDelItem $item */
157 foreach ( $this as $item ) {
158 unset( $missing[$item->getId()] );
160 if ( $perItemStatus ) {
161 $itemStatus = Status
::newGood();
162 $status->itemStatuses
[$item->getId()] = $itemStatus;
164 $itemStatus = $status;
167 $oldBits = $item->getBits();
168 // Build the actual new rev_deleted bitfield
169 $newBits = RevisionDeleter
::extractBitfield( $bitPars, $oldBits );
171 if ( $oldBits == $newBits ) {
172 $itemStatus->warning(
173 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
174 $status->failCount++
;
176 } elseif ( $oldBits == 0 && $newBits != 0 ) {
178 } elseif ( $oldBits != 0 && $newBits == 0 ) {
184 if ( $item->isHideCurrentOp( $newBits ) ) {
185 // Cannot hide current version text
187 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
188 $status->failCount++
;
190 } elseif ( !$item->canView() ) {
191 // Cannot access this revision
192 $msg = ( $opType == 'show' ) ?
193 'revdelete-show-no-access' : 'revdelete-modify-no-access';
194 $itemStatus->error( $msg, $item->formatDate(), $item->formatTime() );
195 $status->failCount++
;
197 // Cannot just "hide from Sysops" without hiding any fields
198 } elseif ( $newBits == Revision
::DELETED_RESTRICTED
) {
199 $itemStatus->warning(
200 'revdelete-only-restricted', $item->formatDate(), $item->formatTime() );
201 $status->failCount++
;
205 // Update the revision
206 $ok = $item->setBits( $newBits );
209 $idsForLog[] = $item->getId();
210 // If any item field was suppressed or unsupressed
211 if ( ( $oldBits |
$newBits ) & $this->getSuppressBit() ) {
212 $logType = 'suppress';
214 // Track which fields where (un)hidden for each item
215 $addedBits = ( $oldBits ^
$newBits ) & $newBits;
216 $removedBits = ( $oldBits ^
$newBits ) & $oldBits;
217 $virtualNewBits |
= $addedBits;
218 $virtualOldBits |
= $removedBits;
220 $status->successCount++
;
221 if ( $wgActorTableSchemaMigrationStage <= MIGRATION_WRITE_BOTH
) {
222 if ( $item->getAuthorId() > 0 ) {
223 $authorIds[] = $item->getAuthorId();
224 } elseif ( IP
::isIPAddress( $item->getAuthorName() ) ) {
225 $authorIPs[] = $item->getAuthorName();
228 if ( $wgActorTableSchemaMigrationStage >= MIGRATION_WRITE_BOTH
) {
229 $authorActors[] = $item->getAuthorActor();
232 // Save the old and new bits in $visibilityChangeMap for
234 $visibilityChangeMap[$item->getId()] = [
235 'oldBits' => $oldBits,
236 'newBits' => $newBits,
240 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
241 $status->failCount++
;
245 // Handle missing revisions
246 foreach ( $missing as $id => $unused ) {
247 if ( $perItemStatus ) {
248 $status->itemStatuses
[$id] = Status
::newFatal( 'revdelete-modify-missing', $id );
250 $status->error( 'revdelete-modify-missing', $id );
252 $status->failCount++
;
255 if ( $status->successCount
== 0 ) {
256 $dbw->endAtomic( __METHOD__
);
260 // Save success count
261 $successCount = $status->successCount
;
263 // Move files, if there are any
264 $status->merge( $this->doPreCommitUpdates() );
265 if ( !$status->isOK() ) {
266 // Fatal error, such as no configured archive directory or I/O failures
267 $lbFactory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
268 $lbFactory->rollbackMasterChanges( __METHOD__
);
274 if ( $wgActorTableSchemaMigrationStage <= MIGRATION_WRITE_BOTH
) {
275 $authorFields['authorIds'] = $authorIds;
276 $authorFields['authorIPs'] = $authorIPs;
278 if ( $wgActorTableSchemaMigrationStage >= MIGRATION_WRITE_BOTH
) {
279 $authorFields['authorActors'] = $authorActors;
284 'title' => $this->title
,
285 'count' => $successCount,
286 'newBits' => $virtualNewBits,
287 'oldBits' => $virtualOldBits,
288 'comment' => $comment,
290 'tags' => $params['tags'] ??
[],
294 // Clear caches after commit
295 DeferredUpdates
::addCallableUpdate(
296 function () use ( $visibilityChangeMap ) {
297 $this->doPostCommitUpdates( $visibilityChangeMap );
299 DeferredUpdates
::PRESEND
,
303 $dbw->endAtomic( __METHOD__
);
308 final protected function acquireItemLocks() {
309 $status = Status
::newGood();
310 /** @var RevDelItem $item */
311 foreach ( $this as $item ) {
312 $status->merge( $item->lock() );
318 final protected function releaseItemLocks() {
319 $status = Status
::newGood();
320 /** @var RevDelItem $item */
321 foreach ( $this as $item ) {
322 $status->merge( $item->unlock() );
329 * Reload the list data from the master DB. This can be done after setVisibility()
330 * to allow $item->getHTML() to show the new data.
332 function reloadFromMaster() {
333 $dbw = wfGetDB( DB_MASTER
);
334 $this->res
= $this->doQuery( $dbw );
338 * Record a log entry on the action
339 * @param string $logType One of (delete,suppress)
340 * @param array $params Associative array of parameters:
341 * newBits: The new value of the *_deleted bitfield
342 * oldBits: The old value of the *_deleted bitfield.
343 * title: The target title
345 * comment: The log comment
346 * authorIds: The array of the user IDs of the offenders
347 * authorIPs: The array of the IP/anon user offenders
348 * authorActors: The array of the actor IDs of the offenders
349 * tags: The array of change tags to apply to the log entry
350 * @throws MWException
352 private function updateLog( $logType, $params ) {
353 // Get the URL param's corresponding DB field
354 $field = RevisionDeleter
::getRelationType( $this->getType() );
356 throw new MWException( "Bad log URL param type!" );
358 // Add params for affected page and ids
359 $logParams = $this->getLogParams( $params );
360 // Actually add the deletion log entry
361 $logEntry = new ManualLogEntry( $logType, $this->getLogAction() );
362 $logEntry->setTarget( $params['title'] );
363 $logEntry->setComment( $params['comment'] );
364 $logEntry->setParameters( $logParams );
365 $logEntry->setPerformer( $this->getUser() );
366 // Allow for easy searching of deletion log items for revision/log items
368 $field => $params['ids'],
370 if ( isset( $params['authorIds'] ) ) {
372 'target_author_id' => $params['authorIds'],
373 'target_author_ip' => $params['authorIPs'],
376 if ( isset( $params['authorActors'] ) ) {
378 'target_author_actor' => $params['authorActors'],
381 $logEntry->setRelations( $relations );
382 // Apply change tags to the log entry
383 $logEntry->setTags( $params['tags'] );
384 $logId = $logEntry->insert();
385 $logEntry->publish( $logId );
389 * Get the log action for this list type
392 public function getLogAction() {
397 * Get log parameter array.
398 * @param array $params Associative array of log parameters, same as updateLog()
401 public function getLogParams( $params ) {
403 '4::type' => $this->getType(),
404 '5::ids' => $params['ids'],
405 '6::ofield' => $params['oldBits'],
406 '7::nfield' => $params['newBits'],
411 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
414 public function clearFileOps() {
418 * A hook for setVisibility(): do batch updates pre-commit.
422 public function doPreCommitUpdates() {
423 return Status
::newGood();
427 * A hook for setVisibility(): do any necessary updates post-commit.
429 * @param array $visibilityChangeMap [id => ['oldBits' => $oldBits, 'newBits' => $newBits], ... ]
432 public function doPostCommitUpdates( array $visibilityChangeMap ) {
433 return Status
::newGood();
437 * Get the integer value of the flag used for suppression
439 abstract public function getSuppressBit();